package com.tm.util;



import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.imageaudit.model.v20191230.ScanImageRequest;
import com.aliyuncs.imageaudit.model.v20191230.ScanImageResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.tm.imgenum.ThImgEnum;
import com.tm.mapper.ImageFilterMapper;
import com.tm.service.ImageFilterService;
import org.springframework.beans.factory.annotation.Autowired;

import java.rmi.ServerException;
import java.util.*;
/**
 * @author Rocky
 * @create 2022-09-18 16:16
 */
public class AliyunImgUtil {

    private static String ALIYUN_ACCESS_KEY_ID = "LTAI5tGAfCbyfRga7ui1U9Xi";

    private static String ALIYUN_ACCESS_KEY_SECRET = "QQQAljx8pZJ42hinBAdK4EGWyBGsbR";

    private static String REGION_ID = "cn-shanghai";

    @Autowired
    private static ImageFilterMapper imageFilterMapper;

    public static void main(String[] args) {

        DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ALIYUN_ACCESS_KEY_ID, ALIYUN_ACCESS_KEY_SECRET);
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/

        IAcsClient client = new DefaultAcsClient(profile);

        ScanImageRequest request = new ScanImageRequest();

        List<ScanImageRequest.Task> taskList = new ArrayList<>();

        ScanImageRequest.Task task1 = new ScanImageRequest.Task();

        task1.setImageURL("https://zkjtest.oss-cn-zhangjiakou.aliyuncs.com/2022-09-13/aaa.png");
        task1.setDataId("67");
        taskList.add(task1);
        request.setTasks(taskList);

        List<String> sceneList = new ArrayList<>();
        sceneList.add("porn");
        sceneList.add("terrorism");
        sceneList.add("ad");
        sceneList.add("live");
        sceneList.add("logo");
        request.setScenes(sceneList);

        try {
            ScanImageResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            String s = new Gson().toJson(response);
            ScanImageResponse.Data data = response.getData();
            List<ScanImageResponse.Data.Result> results = data.getResults();
            ScanImageResponse.Data.Result result = results.get(0);
            List<ScanImageResponse.Data.Result.SubResult> subResults = result.getSubResults();
            StringBuffer flags = new StringBuffer();
            subResults.forEach(a -> {
                if(!a.getSuggestion().equals("pass")){
                    String scene = a.getScene();
                    switch (scene) {
                        case "porn":
                            flags.append(ThImgEnum.PORN.getCode());
                            break;
                        case "ad":
                            flags.append(ThImgEnum.AD.getCode());
                            break;
                        case "terrorism":
                            flags.append(ThImgEnum.TERRORISM.getCode());
                            break;
                        case "logo":
                            flags.append(ThImgEnum.LOGO.getCode());
                            break;
                        case "live":
                            flags.append(ThImgEnum.LIVE.getCode());
                            break;
                        default:
                            break;
                    }
                }
            });
            //flags就有了所有结果
            //12345
            System.out.println(flags);
            //将机器的flags存入数据库中
            //将人工的审核标志改为待审核


        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrorCode());
            System.out.println("ErrMsg:" + e.getErrorMessage());
            System.out.println("RequestId:" + e.getRequestId());
        } catch (com.aliyuncs.exceptions.ServerException e) {
            throw new RuntimeException(e);
        } catch (com.aliyuncs.exceptions.ClientException e) {
            throw new RuntimeException(e);
        }

    }
}
